package cn.minds.capture.dispatch;

import java.io.IOException;

import com.alibaba.fastjson.JSON;

import cn.minds.capture.dispatch.task.Task;

public class Protocol {

	public final static int DEFAULT_TIMEOUT = 30;

	public static void send(CaputureOutputStream outputStream, Task task) throws IOException {
		outputStream.write(JSON.toJSONString(new JsonCommand(task.getClass(), task)));
	}

	public static void receive(CaputureInputStream inputStream, Connect connect) throws IOException {
		String result = null;
		while (connect.isConnected()) {
			if ((result = inputStream.read()) != null && result.length() > 0) {
				JsonCommand command = JSON.parseObject(result, JsonCommand.class);
				Task task = JSON.parseObject(command.getValue(), command.getClazz());
				connect.receiveCommand(task);
			}
		}
	}
}
